#coding=utf-8
from uliweb import expose, functions
from sqlalchemy import or_

def __begin__():
    """
    用户验证
    """
    from uliweb import functions
    return functions.require_login()

@expose('/main')
class Main(object):
    
    def __init__(self):
        from uliweb.orm import get_model
        self.usermodel = get_model('user')
        self.notifymodel = get_model('notify')
    
    #使用构造函数
    @expose('/')
    #@decorators.require_login #验证用户是否登录
    def main(self):
        """
        首页,显示通知数量
        """
        user = request.user
    
        if user.is_superuser:
            #管理员显示自己的和注册类型的
            key = or_(self.notifymodel.c.notify_type == 1,
                      self.notifymodel.c.receiveuser_id == user.id)
    
            ormdata_num = self.notifymodel.filter(key).filter(self.notifymodel.c.senduser_id.in_(self.usermodel.filter().fields('id'))).count()
    
        else:
            #非管理员显示自己相关的
            ormdata_num = self.notifymodel.filter(self.notifymodel.c.receiveuser_id == user.id).filter(self.notifymodel.c.senduser_id.in_(self.usermodel.filter().fields('id'))).count()
    
        return {"num":ormdata_num}

    
    def index(self):
        """
        打开首页，右边内容
        """
        return {}